﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.HttpHelper;
using TmU220PrintHelperV2d0.Singleton;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.Service
{
    public class DatabaseHelperV2d0
    {
        /// <summary>
        /// 读取dp_rms_print_kit_main表和dp_rms_print_kit_detail表，返回主打印数据类（包含打印明细项）
        /// </summary>
        /// <returns></returns>
        public static async Task<List<dp_rms_print_kit_main_TableClass>> GetPrintMain()
        {
            List<dp_rms_print_kit_main_TableClass> resultMainTable = new List<dp_rms_print_kit_main_TableClass>();
            try
            {
                var interfaceInfo = ApiInterfaceInfoSingletonV2d0.Instance.GetApiInterfaceFromName("getDataBySQL");
                string sqlString = "select * from dp_rms_print_kit_main where is_printed=0";
                string interfaceUrl = string.Format(interfaceInfo.InterfaceUrl, sqlString);
                //interfaceUrl = "http://61.244.173.226:90/baidance/public/index.php?s=/api/common/getDatabySQL&SQL=select * from dp_rms_print_kit_main";
                string returnString = await HttpFunctionV2d0.RunApiInterfaceByHttpClient(interfaceUrl, null);
                var result = JsonConvert.DeserializeObject<ApiInterfaceReturnDataPlusV2d0<dp_rms_print_kit_main_TableClass>>(returnString) as ApiInterfaceReturnDataPlusV2d0<dp_rms_print_kit_main_TableClass>;
                resultMainTable = result.content;
                foreach (var mainRecord in resultMainTable)
                {
                    string mainId = mainRecord.id.ToString();
                    sqlString = "select * from dp_rms_print_kit_detail where main_id=" + "'" + mainId + "'";
                    interfaceUrl = string.Format(interfaceInfo.InterfaceUrl, sqlString);
                    string resultDetailString = await HttpFunctionV2d0.RunApiInterfaceByHttpClient(interfaceUrl, null);
                    var resultDetail = JsonConvert.DeserializeObject<ApiInterfaceReturnDataPlusV2d0<dp_rms_print_kit_detail_TableClass>>(resultDetailString) as ApiInterfaceReturnDataPlusV2d0<dp_rms_print_kit_detail_TableClass>;
                    mainRecord.detailList = resultDetail.content;
                }
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
            return resultMainTable;
        }
        /// <summary>
        /// 跟新dp_rms_print_kit_main，将id的is_printed设置为1，已打印
        /// </summary>
        /// <param name="id"></param>
        public static void UpdatePrintMainIsPrintedTrue(Guid id)
        {
            try
            {
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
        }


    }
}
